package com.csj.DesignPattern.obverser;

import java.util.ArrayList;
import java.util.List;

public class SubscriptionSubject implements Subject{

    private List<Observer> weixinUserList = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        weixinUserList.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        weixinUserList.remove(observer);
    }

    @Override
    public void notify(String message) {
        for (Observer observer : weixinUserList) {
            observer.update(message);
        }
    }
}

/*

    同Observable
public class Observable {
    //同List<Observer> weixinUserList
    private Vector<Observer> obs;
    public Observable() {
        obs = new Vector<>();
    }

    public synchronized void addObserver(Observer o) {
        //若没有改观察者，则添加到集合中
        if (!obs.contains(o)) {
            obs.addElement(o);
        }
    }

    public void notifyObservers(Object arg) {
        //changed为判断标识，只有当changed为true时，才能通知观察者，所以在通知之前需要调用setChanged方法
        if (!changed)
            return;
        arrLocal = obs.toArray();
        //将changed为false
        clearChanged();
        for (int i = arrLocal.length-1; i>=0; i--)
            //调用观察者中的update方法
            ((Observer)arrLocal[i]).update(this, arg);
    }

}
 */